package org.itboys.mobile.controller.weixin.cart;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.struts.util.RequestUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.count.MdlCountDto;
import org.itboys.mobile.dto.member.SundayWeixinMemberDto;
import org.itboys.mobile.service.mongo.weixin.cart.MdlWeixinCartService;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/12/7
 * 描述：迈迪龙_微信_购物车
 */
@Controller
@RequestMapping("/mdl/weixin/cart")
public class MdlWeixinCartController extends BaseController{
    @Autowired
    private MdlWeixinCartService cartService;

    /**
     * 【用户版和管理版】加入购物车（支持批量新增）。商品ID集合和数量集合，需要对应。
     * @param request
     * @param response
     * @param productIds
     * @param nums
     */
    @RequestMapping(value="/addCart",method= RequestMethod.POST)
    public void addCart(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(required = true, name = "productIds", value = "商品ID集合，和数量集合对应。如：productIds=1,2,3") @RequestParam(value = "productIds", required = true) List<Long> productIds,
                        @ApiParam(required = true, name = "nums", value = "数量集合，和商品ID对应。如：nums=1,2,3") @RequestParam(value = "nums", required = true) List<Integer> nums
    ) {
        try{


            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            cartService.addCart(member.getId(), productIds , nums, SundayCommonConstants.cart_type_cart);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/weixin/cart/addCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 获取购物车。购物车ID集合参数说明：可传入此参数，核算购物车总价
     * @param request
     * @param response
     * @param cartIds

     */
    @RequestMapping(value="/getCart",method= RequestMethod.POST)
    public void getCart(HttpServletRequest request,
                        HttpServletResponse response,
                        @ApiParam(required = false, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3。") @RequestParam(value = "cartIds", required = false) List<Long> cartIds
    ) {

        try{
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            MdlCountDto result = cartService.getCart(member.getId(), cartIds, SundayCommonConstants.cart_type_cart);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/weixin/cart/getCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


    /**
     * 修改购物车数量（支持批量修改）。商品ID集合和数量集合，需要对应
     * @param request
     * @param response
     * @param cartIds
     * @param nums
     */
    @RequestMapping(value="/updateCart",method= RequestMethod.POST)
    public void updateCart(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds,
                           @ApiParam(required = true, name = "nums", value = "数量集合（修改后的数量），和购物车ID对应。如：numbers=1,2,3") @RequestParam(value = "nums", required = true) List<Integer> nums
    ) {

        try{
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            cartService.updateCart( member.getId(), cartIds ,nums);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/weixin/cart/updateCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 修改购物车数量（支持批量修改）。商品ID集合和数量集合，需要对应
     * @param request
     * @param response
     * @param cartIds
     */
    @RequestMapping(value="/deleteCart",method= RequestMethod.POST)
    public void deleteCart(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds
    ) {

        try{
            SundayWeixinMemberDto member=HttpRequestUtil.getUser(request);
            cartService.deleteCart(cartIds , member.getId(),SundayCommonConstants.cart_type_cart);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/mdl/weixin/cart/deleteCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
